#include "test.h"
#include <nxos.h>

// #define CONFIG_TEST_SCAN

static void test_print(void)
{
    printf("hello, test stdio\n hello, world!\n");
    int n0 = 12345678;
    int n1 = 0x12345678;
    char *s0 = "12345678";
    
    printf("test args, int %d str %s hex %x ptr %p\n", n0, s0, n1, &s0);
    printf("test args, int %d str %s hex %x ptr %p\n", 12345678, "12345678", 0x12345678, 0x12345678);

    putchar('A');
    putchar('1');
    putchar('#');
    putchar('\n');

    puts("abcABC123\n");
    
    perror("test stdio error! It's OK!");

    fflush(stdout);
}

#if defined(CONFIG_TEST_SCAN)
static void test_scan(void)
{
    printf("ready getchar:\n");
    int ch = getchar();
    printf("get char:%d/%x/%c\n", ch, ch, ch);
    
    char s[32] = {0};
    gets(s);
    printf("get str:%s\n", s);
    
    int a, b, c;

    printf("请输入三个数字：");
    scanf("%d%d%d",&a,&b,&c); 
    printf("%d,%d,%d\n",a,b,c);

    printf("请输入三个字符：");
    scanf("%c%c%c",&a,&b,&c); 
    printf("%c,%c,%c\n", a,b,c);

    char str1[20], str2[30];

    printf("请输入用户名：");
    scanf("%s", str1);
    
    printf("请输入您的网站：");
    scanf("%s", str2);
    
    printf("输入的用户名：%s\n", str1);
    printf("输入的网站：%s", str2);
}
#endif

static void test_file_read(void)
{
    char * path = "/testlibc";

    FILE * fp = fopen(path, "r");
    if (!fp)
    {
        fprintf(stderr, "read open %s error\n", path);
        return;
    }
    
    printf("read open file:%s ok\n", path);

    char buf[1024];
    size_t len;
    len = fread(buf, 1024 ,1, fp);
    printf("1 read len %d\n", len);

    printf("dump buf %s\n", buf);

    len = fread(buf, 1024 ,1, fp);
    printf("2 read len %d\n", len);

    printf("dump buf %s\n", buf);

    fseek(fp, 0, SEEK_SET);

    len = fread(buf, 1024 ,1, fp);
    printf("3 read again len %d\n", len);

    printf("dump buf %s\n", buf);

    len = fread(buf, 1024 ,1, fp);
    printf("4 read len %d\n", len);

    printf("dump buf %s\n", buf);


    fseek(fp, 0, SEEK_SET);

    len = fread(buf, 1024 / 8,8, fp);
    printf("1 read len %d\n", len);

    printf("dump buf %s\n", buf);

    len = fread(buf, 1024 / 8,8, fp);
    printf("2 read len %d\n", len);

    printf("dump buf %s\n", buf);

    fseek(fp, 0, SEEK_SET);

    len = fread(buf, 1024 / 8,8, fp);
    printf("3 read again len %d\n", len);

    printf("dump buf %s\n", buf);

    len = fread(buf, 1024 / 8,8, fp);
    printf("4 read len %d\n", len);

    printf("dump buf %s\n", buf);

    fclose(fp);
}

static void test_file_write(void)
{
    char * path = "/stdio.w.tmp";

    FILE * fp = fopen(path, "w+");
    if (!fp)
    {
        fprintf(stderr, "write open %s error\n", path);
        return;
    }
    
    printf("write open file:%s ok\n", path);

    char * s = "hello, world!\nhello, abc!\n1234 1234 abcd\n!!!";

    size_t len;
    len = fwrite(s, strlen(s) ,1, fp);
    printf("1 write len %d\n", len);

    len = fwrite(s, 1, strlen(s), fp);
    printf("1 write len %d\n", len);
    
    fclose(fp);
    
    fp = fopen(path, "r");
    if (!fp)
    {
        fprintf(stderr, "read open %s error\n", path);
        return;
    }
    
    printf("read open file:%s ok\n", path);

    char buf[1024];

    len = fread(buf, 1024 ,1, fp);
    printf("1 read len %d\n", len);

    printf("dump buf: %s\n", buf);

    fseek(fp, 0, SEEK_SET);

    len = fread(buf, 1, 1024, fp);
    printf("2 read again len %d\n", len);

    printf("dump buf: %s\n", buf);

    fclose(fp);
}

static void test_file_rename(void)
{
    printf("rename ret: %d\n", rename("/stdio.w.tmp", "/stdio.w.tmp.2"));
    printf("rename ret: %d\n", rename("/stdio.w.tmp.2", "/stdio.w.tmp"));
}

static void test_file_remove(void)
{
    printf("remove ret: %d\n", remove("/stdio.w.tmp"));
}

static void test_tmpnam(void)
{
    char buffer[L_tmpnam];
    char *ptr;

    tmpnam(buffer);
    printf("临时名称 1: %s\n", buffer);
    
    ptr = tmpnam(NULL);
    printf("临时名称 2: %s\n", ptr);
}

static void test_tmpfile(void)
{
    FILE *fp;

    NX_DirCreate("/tmp", 0666);

    fp = tmpfile();
    printf("临时文件被创建\n");
    if (!fp)
    {
        printf("create tmp file failed!\n");
        return;
    }

    fclose(fp);
}

int stdio_test(int argc, char *argv[])
{
    printf("test stdio\n");
    
    test_print();
#if defined(CONFIG_TEST_SCAN)
    test_scan();
#endif
    test_file_write();
    test_file_write();
    test_file_read();
    test_file_read();
    test_file_rename();
    test_file_remove();
    test_tmpnam();
    test_tmpfile();

    return 0;
}


